home *** CD-ROM | disk | FTP | other *** search
- /*
- ** SendText.c
- **
- ** Text sending support routines
- **
- ** Copyright © 1990-1996 by Olaf `Olsen' Barthel
- ** All Rights Reserved
- */
-
- #ifndef _GLOBAL_H
- #include "Global.h"
- #endif
-
- /* Local variables. */
-
- STATIC LONG WaitCount,PromptCount;
-
- /* MatchPrompt():
- *
- * Search incoming data stream for a match.
- */
-
- BOOL
- MatchPrompt(register STRPTR Data,register LONG Size,register STRPTR Prompt,register LONG PromptLen)
- {
- register UBYTE c,Mask;
-
- if(!Data)
- {
- WaitCount = PromptCount = 0;
-
- return(FALSE);
- }
-
- if(Config -> SerialConfig -> StripBit8)
- Mask = 0x7F;
- else
- Mask = 0xFF;
-
- do
- {
- if(c = ((*Data++) & Mask))
- {
- register BOOL MatchMade;
-
- do
- {
- MatchMade = FALSE;
-
- if(PromptCount == WaitCount)
- {
- if(c == Prompt[WaitCount] & Mask)
- {
- MatchMade = TRUE;
-
- if(PromptLen == ++PromptCount)
- return(TRUE);
- }
- }
-
- if(MatchMade)
- WaitCount++;
- else
- {
- if(WaitCount)
- {
- WaitCount = 0;
-
- PromptCount = 0;
- }
- else
- break;
- }
- }
- while(!WaitCount);
- }
- }
- while(--Size);
-
- return(FALSE);
- }
-
- /* LocalWaitForPrompt(STRPTR Prompt,LONG PromptLen):
- *
- * Scan the incoming data flow for a certain string.
- */
-
- STATIC BOOL
- LocalWaitForPrompt(STRPTR Prompt,LONG PromptLen)
- {
- ULONG Signals;
-
- MatchPrompt(NULL,0,NULL,0);
-
- if(DataHold)
- {
- DataHold = NULL;
-
- RestartSerial();
- }
-
- if(CheckSerialRead())
- Signals = SIG_SERIAL;
- else
- Signals = NULL;
-
- StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
-
- for(;;)
- {
- if(Signals & SIG_SERIAL)
- {
- if(!WaitSerialRead())
- {
- LONG Length;
-
- BytesIn++;
-
- if(Translate_CR_LF)
- Length = (* Translate_CR_LF)(ReadBuffer,1);
- else
- Length = 1;
-
- if(Length)
- {
- ConProcess(ReadBuffer,Length);
-
- Status = STATUS_READY;
-
- if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
- {
- StopTime();
-
- RestartSerial();
-
- return(TRUE);
- }
- }
-
- do
- {
- /* Check how many bytes are still in
- * the serial buffer.
- */
-
- if(Length = GetSerialWaiting())
- {
- if(Length > SerialBufferSize)
- Length = SerialBufferSize;
-
- if(!DoSerialRead(ReadBuffer,Length))
- {
- BytesIn += Length;
-
- if(Translate_CR_LF)
- Length = (* Translate_CR_LF)(ReadBuffer,Length);
-
- if(Length)
- {
- ConProcess(ReadBuffer,Length);
-
- Status = STATUS_READY;
-
- if(MatchPrompt(ReadBuffer,Length,Prompt,PromptLen))
- {
- StopTime();
-
- RestartSerial();
-
- return(TRUE);
- }
- }
- }
- }
- }
- while(Length);
- }
-
- RestartSerial();
- }
-
- if(Signals & SIG_TIMER)
- {
- WaitTime();
-
- return(FALSE);
- }
-
- Signals = Wait(SIG_SERIAL | SIG_TIMER);
- }
- }
-
- /* SendLinePrompt(STRPTR Line,LONG Len):
- *
- * Send text line, wait for prompt.
- */
-
- BOOL
- SendLinePrompt(STRPTR Line,LONG Len)
- {
- LONG i;
-
- if(Len == -1)
- Len = strlen(Line);
-
- while(Len)
- {
- i = 0;
-
- while(i < Len && Line[i] != '\r')
- i++;
-
-
- if(Line[i] == '\r')
- {
- i++;
-
- SerWrite(Line,i);
-
- if(!LocalWaitForPrompt(SendPrompt,SendPromptLen))
- return(FALSE);
- }
- else
- {
- if(i)
- SerWrite(Line,i);
- }
-
- Len -= i;
- Line += i;
- }
-
- return(TRUE);
- }
-
- /* SendLineSimple(STRPTR Line,LONG Len):
- *
- * Send a text line, no fancy features.
- */
-
- BOOL
- SendLineSimple(STRPTR Line,LONG Len)
- {
- if(Len == -1)
- Len = strlen(Line);
-
- SerWrite(Line,Len);
-
- return(TRUE);
- }
-
- /* SendLineDial(STRPTR Line,LONG Len):
- *
- * The SendLineSimple for the dialer and init commands.
- */
-
- BOOL
- SendLineDial(STRPTR Line,LONG Len)
- {
- if(Len == -1)
- Len = strlen(Line);
-
- if(Config -> ModemConfig -> CharSendDelay)
- {
- ULONG Seconds = Config -> ModemConfig -> CharSendDelay / MILLION,
- Micros = Config -> ModemConfig -> CharSendDelay % MILLION;
-
- while(Len--)
- {
- SerWrite(Line++,1);
-
- DelayTime(Seconds,Micros);
- }
- }
- else
- SerWrite(Line,Len);
-
- return(TRUE);
- }
-
- /* SendLineDelay(STRPTR Line,LONG Len):
- *
- * Send a text line, include a delay where necessary.
- */
-
- BOOL
- SendLineDelay(STRPTR Line,LONG Len)
- {
- if(Len == -1)
- Len = strlen(Line);
-
- while(Len--)
- {
- SerWrite(Line,1);
-
- if(*Line == '\r')
- DelayTime(Config -> ClipConfig -> LineDelay / 100,(Config -> ClipConfig -> LineDelay % 100) * 10000);
- else
- DelayTime(Config -> ClipConfig -> CharDelay / 100,(Config -> ClipConfig -> CharDelay % 100) * 10000);
-
- Line++;
- }
-
- return(TRUE);
- }
-
- /* SendLineEcho(STRPTR Line,LONG Len):
- *
- * Send a text line, wait for single characters to be echoed.
- */
-
- BOOL
- SendLineEcho(STRPTR Line,LONG Len)
- {
- ULONG Signals;
-
- if(DataHold)
- {
- DataHold = NULL;
-
- RestartSerial();
- }
-
- if(Len == -1)
- Len = strlen(Line);
-
- while(Len--)
- {
- SerWrite(Line,1);
-
- StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
-
- FOREVER
- {
- Signals = Wait(SIG_TIMER | SIG_SERIAL);
-
- if(Signals & SIG_SERIAL)
- {
- if(!WaitSerialRead())
- {
- LONG Length;
-
- if(Translate_CR_LF)
- Length = (* Translate_CR_LF)(ReadBuffer,1);
- else
- Length = 1;
-
- if(Length > Len)
- Length = Len;
-
- if(Length)
- {
- if(!memcmp(ReadBuffer,Line,Length))
- {
- StopTime();
-
- RestartSerial();
-
- break;
- }
- }
-
- RestartSerial();
- }
- else
- {
- StopTime();
-
- RestartSerial();
-
- return(FALSE);
- }
- }
-
- if(Signals & SIG_TIMER)
- {
- WaitTime();
-
- return(FALSE);
- }
- }
-
- Line++;
- }
-
- return(TRUE);
- }
-
- /* SendLineAnyEcho(STRPTR Line,LONG Len):
- *
- * Send a text line, wait for characters to be echoed.
- */
-
- BOOL
- SendLineAnyEcho(STRPTR Line,LONG Len)
- {
- ULONG Signals;
-
- if(DataHold)
- {
- DataHold = NULL;
-
- RestartSerial();
- }
-
- if(Len == -1)
- Len = strlen(Line);
-
- while(Len--)
- {
- SerWrite(Line,1);
-
- StartTime(Config -> ClipConfig -> SendTimeout / 100,(Config -> ClipConfig -> SendTimeout % 100) * 10000);
-
- FOREVER
- {
- Signals = Wait(SIG_TIMER | SIG_SERIAL);
-
- if(Signals & SIG_SERIAL)
- {
- if(!WaitSerialRead())
- {
- StopTime();
-
- RestartSerial();
-
- break;
- }
- else
- {
- StopTime();
-
- RestartSerial();
-
- return(FALSE);
- }
- }
-
- if(Signals & SIG_TIMER)
- {
- WaitTime();
-
- return(FALSE);
- }
- }
-
- Line++;
- }
-
- return(TRUE);
- }
-
- /* SendLineKeyDelay(STRPTR Line,LONG Len):
- *
- * Send a text line, include keyboard delay pauses between characters.
- */
-
- BOOL
- SendLineKeyDelay(STRPTR Line,LONG Len)
- {
- struct Preferences Prefs;
-
- if(Len == -1)
- Len = strlen(Line);
-
- /* Get current key repeat delay. */
-
- GetPrefs(&Prefs,offsetof(struct Preferences,KeyRptDelay));
-
- /* Any delay specified at all? */
-
- if(Prefs . KeyRptSpeed . tv_secs || Prefs . KeyRptSpeed . tv_micro)
- {
- while(Len--)
- {
- SerWrite(Line++,1);
-
- if(Len)
- DelayTime(Prefs . KeyRptSpeed . tv_secs,Prefs . KeyRptSpeed . tv_micro);
- }
- }
- else
- SerWrite(Line,Len);
-
- return(TRUE);
- }
-
- /* SendSetup():
- *
- * Choose the right routine for the text line output job.
- */
-
- VOID
- SendSetup()
- {
- /* Prepare the prompt string. */
-
- if(Config -> ClipConfig -> LinePrompt[0])
- SendPromptLen = TranslateString(Config -> ClipConfig -> LinePrompt,SendPrompt);
- else
- {
- SendPrompt[0] = 0;
- SendPromptLen = 0;
- }
-
- /* Pick the line send routine. */
-
- switch(Config -> ClipConfig -> PacingMode)
- {
- case PACE_DIRECT:
-
- SendLine = SendLineSimple;
- break;
-
- case PACE_ECHO:
-
- if(Config -> ClipConfig -> SendTimeout)
- SendLine = SendLineEcho;
- else
- SendLine = SendLineSimple;
-
- break;
-
- case PACE_ANYECHO:
-
- if(Config -> ClipConfig -> SendTimeout)
- SendLine = SendLineAnyEcho;
- else
- SendLine = SendLineSimple;
-
- break;
-
- case PACE_PROMPT:
-
- if(Config -> ClipConfig -> SendTimeout)
- SendLine = SendLinePrompt;
- else
- SendLine = SendLineSimple;
-
- break;
-
- case PACE_DELAY:
-
- if(Config -> ClipConfig -> LineDelay || Config -> ClipConfig -> CharDelay)
- SendLine = SendLineDelay;
- else
- SendLine = SendLineSimple;
-
- break;
-
- case PACE_KEYBOARD:
-
- SendLine = SendLineKeyDelay;
- break;
- }
- }
-